package com.ysxsoft.wdg.widget;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.socks.library.KLog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.util.ToastUtils;

/**
 * 弹出第三方分享的PopupWindow
 */
public class SharePopupWindow extends PopupWindow implements View.OnClickListener {
    private final String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private ImageView wechatFriend, qqFriend, weChatRoom, qqRoom;
    private TextView close;

    private ShareAction mShareAction;
    private String channel;

    //控制分享可不可用
    private boolean isInit = false;
    private UMShareAPI mShareAPI;

    private int code = 0;


    /**
     * 分享title、内容、图片、链接、
     */
    private String title, content, linkHref, picUrl;
    private String picUrlId = "http://wdg.sanzhima.cn/woniu/public/static/admin/img/my.png";
    private View rootView;
    private UMImage image;


    public SharePopupWindow(Context context, UMShareAPI mShareAPI) {
        super(context);
        mContext = context;
        this.mShareAPI = mShareAPI;

        initPopup();
        bindView();
        setListener();
    }

    private void initPopup() {
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    private void bindView() {
        rootView = LayoutInflater.from(mContext).inflate(R.layout.layout_share, null);
        qqFriend =  rootView.findViewById(R.id.iv_qq);
        wechatFriend =   rootView.findViewById(R.id.iv_wx);
        weChatRoom =  rootView.findViewById(R.id.iv_friend);
        qqRoom =  rootView.findViewById(R.id.iv_qqK);
        close = (TextView) rootView.findViewById(R.id.item_cancel);
        setContentView(rootView);

    }

    private void setListener() {
        qqFriend.setOnClickListener(this);
        qqRoom.setOnClickListener(this);
        wechatFriend.setOnClickListener(this);
        weChatRoom.setOnClickListener(this);
        close.setOnClickListener(this);
    }


    /**
     * 设置分享的数据，在网络请求之后
     *
     * @param title    标题
     * @param content  简介
     * @param linkHref 链接
     * @param picUrl
     * @param code     0:本地图片  1:网址
     */
    public void setShareData(String title, String content, String linkHref, String picUrl, int code) {
        this.title = title;
        this.content = content;
        this.linkHref = linkHref;
        if (!TextUtils.isEmpty(picUrl)) {
            this.picUrl = picUrl;
        }
        this.code = code;
        isInit = true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_wx://微信好友
                if (isInit) {
                    if (mShareAPI.isInstall((Activity) mContext, SHARE_MEDIA.WEIXIN)) {
                        startShare(SHARE_MEDIA.WEIXIN);
                        channel = "1";
                    } else {
                        ToastUtils.showToast("微信未安装");
                        KLog.e("微信未安装");
                    }
                } else {
                    KLog.e(TAG, "onClick: not init");
                }
                break;

            case R.id.iv_friend://微信朋友圈
                if (isInit) {
                    if (mShareAPI.isInstall((Activity) mContext, SHARE_MEDIA.WEIXIN_CIRCLE)) {
                        startShare(SHARE_MEDIA.WEIXIN_CIRCLE);
                        channel = "2";
                    } else {
                        ToastUtils.showToast("微信未安装");
                        KLog.e("微信未安装");
                    }
                } else {
                    KLog.e(TAG, "onClick: not init");
                }
                break;

            case R.id.iv_qq://QQ
                if (isInit) {
                    if (mShareAPI.isInstall((Activity) mContext, SHARE_MEDIA.QQ)) {
                        startShare(SHARE_MEDIA.QQ);
                        channel = "4";
                    } else {
                        ToastUtils.showToast("QQ未安装");
                        KLog.e("QQ未安装");
                    }
                } else {
                    KLog.e(TAG, "onClick: not init");
                }
                break;
            case R.id.iv_qqK://QQ空间
                if (isInit) {
                    if (mShareAPI.isInstall((Activity) mContext, SHARE_MEDIA.QQ)) {
                        startShare(SHARE_MEDIA.QZONE);
                        channel = "5";
                    } else {
                        ToastUtils.showToast("QQ未安装");
                        KLog.e("QQ未安装");
                    }
                } else {
                    KLog.e(TAG, "onClick: not init");
                }
                break;
        }
        dismiss();
    }

    private void startShare(SHARE_MEDIA share_media) {
        mShareAction = new ShareAction((Activity) mContext);
        UMWeb umWeb = null;
        if (code == 0) {
            umWeb = getUmWeb(mContext, linkHref, title, content);
            mShareAction.setPlatform(share_media)//传入平台
                    .withMedia(umWeb)
                    .setCallback(mUmShareListener)//回调监听器
                    .share();

        }  else if (code == 1) {//分享图片
            UMImage image = new UMImage(mContext, picUrl);
            mShareAction.setPlatform(share_media)//传入平台
                    .withMedia(image)
                    .withText(content)
                    .setCallback(mUmShareListener)//回调监听器
                    .share();
            return;
        }

    }


    /**
     * @param context 上下文
     * @param url     链接地址
     * @param title   标题
     * @param desc    描述
     * @return
     */
    public UMWeb getUmWeb(Context context, String url, String title, String desc) {
        UMWeb web = new UMWeb(url);
        web.setTitle(title);
        web.setDescription(desc);
        UMImage image;
        if (!TextUtils.isEmpty(picUrl)){
            image = new UMImage(context, picUrl);
        }else {
            image = new UMImage(context, picUrlId);
        }
        web.setThumb(image);
        return web;
    }

    //显示popWindow
    public void show(View view) {
        showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        WindowManager.LayoutParams attributes = ((Activity) mContext).getWindow().getAttributes();
        attributes.alpha = 0.7f;
        ((Activity) mContext).getWindow().setAttributes(attributes);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams attributes = ((Activity) mContext).getWindow().getAttributes();
        attributes.alpha = 1f;
        ((Activity) mContext).getWindow().setAttributes(attributes);
        closeKeyboard();
    }

    private void closeKeyboard() {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(((Activity) mContext).getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    public interface onShareClickListener {
        void onShareClick(SHARE_MEDIA share_media);
    }


    //分享回调
    private UMShareListener mUmShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastUtils.showToast("分享成功!");
            //postShareResult();//请求分享结果
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtils.showToast("分享失败!");
        }

        @Override
        public void onCancel(SHARE_MEDIA platfm) {
            ToastUtils.showToast("分享取消!");
        }
    };
}
